//package com.dataseek.iot.auth.manager.web;
//
//import com.dataseek.iot.core.constant.TopicConst;
//import com.dataseek.iot.core.util.DataUtil;
//import com.dataseek.iot.mqtt.config.MqttBoundConfiguration;
//import com.dataseek.iot.mqtt.service.MqttGateway;
//import com.dataseek.iot.mysql.entity.Device;
//import com.dataseek.iot.mysql.service.impl.DeviceServiceImpl;
//import com.dataseek.iot.mysql.service.impl.UserServiceImpl;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.eclipse.paho.client.mqttv3.MqttException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.server.ServerWebExchange;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Objects;
//
///**
// * @author cc
// * @Title: 连接服务接口
// * @Description: 连接服务接口
// * @date 2021/11/115:52 下午
// * @since 1.0
// */
//@RestController
//@RequestMapping("/connection")
//@Api(tags = "连接服务接口")
//@Slf4j
//public class ConnectionController {
//
//
//    @Value("${mqtt.publisher.clientId}")
//    private String platformPublisher;
//
//    @Value("${mqtt.subscriber.clientId}")
//    private String platformSubscriber;
//
//    @Value("${mqtt.device.secureId}")
//    private String secureId;
//
//    @Value("${mqtt.device.secureKey}")
//    private String secureKey;
//
//    @Autowired
//    MqttGateway mqttGateway;
//
//    @Autowired
//    DeviceServiceImpl deviceService;
//
//    @Autowired
//    UserServiceImpl userService;
//
//    @Autowired
//    MqttBoundConfiguration mqttBoundConfiguration;
//
//    @GetMapping("/mqtt/auth")
//    @ApiOperation("/MQTT 登陆鉴权")
//    public ResponseEntity<?> authMqtt(ServerWebExchange exchange) throws MqttException {
//        MultiValueMap<String, String> maps = exchange.getRequest().getQueryParams();
//        String clientid;String username;String password;StringBuffer sb = null;
//
//        if (maps.containsKey("clientid") && maps.containsKey("username") && maps.containsKey("password")) {
//            clientid = maps.getFirst("clientid");
//            username = maps.getFirst("username");
//            password = maps.getFirst("password");
//            log.info("clientId :" + clientid + ", username : " + username + ", password: " + password);
//        }else {
//            sb.append("设备认证参数错误，请核查!");
//            //发送通知消息
//            mqttGateway.sendToMqtt(TopicConst.NOTICE, sb.toString());
//
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//        }
//
//        //平台认证
//        if (clientid.equals(platformPublisher) || clientid.equals(platformSubscriber)) {
//            //TODO 运行记录
//            log.info("平台开始运行");
//            return ResponseEntity.status(HttpStatus.OK).build();
//        } else {
//            String[] temp = clientid.split("-");
//            switch (temp.length - 1) {
//                //前端认证
//                case 1:
//                    String webCode = temp[0];
//                    //TODO 操作记录
//                    log.info("用户编码为:" + webCode + " 的用户开始使用");
//                    sb = new StringBuffer();
//
//                    //发送通知消息
//                    //mqttGateway.sendToMqtt(TopicConst.NOTICE, sb.toString());
//                    return ResponseEntity.status(HttpStatus.OK).build();
//                //设备认证
//                case 0:
//                    String deviceCode = temp[0];
//                    //TODO 校验账号
//                    Device d = deviceService.getDeviceByCode(deviceCode);
//                    sb = new StringBuffer();
//                    sb.append("名称为: ").append(d.getDeviceName())
//                            .append(" 编码为: ");
//                    if (Objects.nonNull(d) && !d.getState().equals("-1")) {
//                        sb.append(d.getDeviceCode()).append(" 的设备已经上线，请检查设备编码是否重复");
//                    } else if (Objects.nonNull(username) && Objects.nonNull(password)) {
//                        String _secureId = username.split("|")[0];
//                        String _password = DataUtil.stringToMD5(username + "|" + secureKey);
//                        if (_secureId.equals(secureId) && _password.equals(password)) {
//                            sb.append(" 的设备认证成功");
//                        } else {
//                            sb.append(" 的设备认证失败");
//                        }
//                        log.info(sb.toString());
//                    }
//                    //发送通知消息
//                    mqttGateway.sendToMqtt(TopicConst.NOTICE, sb.toString());
//                    return ResponseEntity.status(HttpStatus.OK).build();
//                //错误
//                default:
//                    sb.append("设备认证参数错误，请核查!");
//                    //发送通知消息
//                    mqttGateway.sendToMqtt(TopicConst.ALARM, sb.toString());
//                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//            }
//        }
//    }
//}
